package com.example.popupwindowdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Button b = findViewById(R.id.button2);

        PopupWindow popupWindow = new PopupWindow(this);
        View v = getLayoutInflater().inflate(R.layout.layout_popup, null);
        popupWindow.setContentView(v);

        popupWindow.setOutsideTouchable(true);//设置点击外部区域可以取消popupWindow

        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.showAsDropDown(view, 0, 0);//设置popupWindow显示,并且告诉它显示在那个View下面

            }
        });

    }
}